package com.example.demo.service.impl;

import com.example.demo.entity.Cart;
import com.example.demo.entity.Menu;
import com.example.demo.exception.BusinessException;
import com.example.demo.exception.CodeEnum;
import com.example.demo.mapper.CartMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author chenqiuran
 * @decription
 * @date 2021/8/31
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean addCart(Cart cart) {

        if(cartMapper.existCart(cart.getUserId(),cart.getMenuId())){
            //如果存在就更新
            cart.setUpdater(userMapper.findById(cart.getUserId()).getUsername());
            cart.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            return cartMapper.updateCart(cart)>0;
        }else {
            //如果不存在就添加
            return cartMapper.addCart(cart)>0;
        }
    }

    @Override
    public List<Cart> findAll(Long userId) {
        return cartMapper.findAll(userId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean productAmountASC(Long userId, Long menuId,Integer amount) {
        return cartMapper.productAmountASC(userId,menuId,amount)>0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean productAmountDESC(Long userId, Long menuId) {
        return cartMapper.productAmountDESC(userId,menuId)>0;
    }

    @Override
    public Cart findById(Long userId, Long menuId) {
        return cartMapper.findById(userId,menuId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean delCart(Long userId, Long menuId) throws BusinessException {
        if(cartMapper.existCart(userId,menuId)){
            return cartMapper.delCart(userId,menuId)>0;
        }else {
            throw new BusinessException(CodeEnum.NOT_EXIST_CART.getMsg(),CodeEnum.NOT_EXIST_CART.getCode());
        }
    }

    /**
     * @Author chenqiuran
     * @Description //TODO 更新购物车
     * @Date 18:14 2021/9/2
     * @Param [cart]
     * @return java.lang.Boolean
     **/
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateCart(Cart cart) throws BusinessException {
        //判断是否存在购物车
        if(cartMapper.existCart(cart.getUserId(),cart.getMenuId())){
            //更新购物车
            cart.setUpdater(userMapper.findById(cart.getUserId()).getUsername());
            cart.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            return cartMapper.updateCart(cart)>0;
        }else {
            //抛异常：不存在该购物车
             throw new BusinessException(CodeEnum.NOT_EXIST_CART.getMsg(),CodeEnum.NOT_EXIST_CART.getCode());
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addAllCart(List<Cart> cartList) {
        for (Cart cart : cartList) {
            if(cartMapper.existCart(cart.getUserId(),cart.getMenuId())){
                //如果存在就更新
                cart.setUpdater(userMapper.findById(cart.getUserId()).getUsername());
                cart.setUpdateTime(new Timestamp(System.currentTimeMillis()));
                cartMapper.updateCart(cart);
            }else {
                //如果不存在就添加
                cartMapper.addCart(cart);
            }
        }
    }

//    @Autowired
//    private CartMapper cartMapper;
//
//    @Override
//    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
//    //添加商品到购物车
//    public Integer addCart(Cart cart) {
//        return cartMapper.addCart(cart);
//    }
//
//    @Override
//    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
//    //查看购物车所有菜品
//    public List<Menu> findAll(Long userId) {
//        System.out.println(cartMapper.findAll(userId));
//        return cartMapper.findAll(userId);
//    }
//
//    @Override
//    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
//    //增加菜品数量
//    public Integer productAmountASC(Long cartId) {
//        return cartMapper.productAmountASC(cartId);
//    }
//
//    @Override
//    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
//    //减少菜品数量
//    public Integer productAmountDESC(Long cartId) {
//        return cartMapper.productAmountDESC(cartId);
//    }
//
//    @Override
//    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
//    //清空购物车
//    public Integer delCart(Long userId) {
//        return cartMapper.delCart(userId);
//    }
//
//    //根据Id查找购物车菜品
//    @Override
//    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
//    public Cart findById(Long cartId){
//        return cartMapper.findById(cartId);
//    }

}
